package com.fingard.dsp.bank.directbank.abc03;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.abc02.ABC02Header;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class ABC03Base extends DirectBase {
    protected String txCode = "";
    protected String reqSeqID;
    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }
    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime17SSSFormat.format(tmpDateNow);
    }
    protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception{
        if (StringHelper.isNullOrEmpty(txCode) || StringHelper.isNullOrEmpty(reqSeqID)) {
            throw new Exception("未赋值txCode或reqSeqID");
        }
        Date date = new Date();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("TX");
        xmlWriter.writeStartElement("TX_HEADER");
        xmlWriter.writeElementString("SYS_HDR_LEN", "0", true);//<TX_HEADER>节点内容的长度（包括节点本身）
        xmlWriter.writeElementString("SYS_PKG_VRSN", "01");
        xmlWriter.writeElementString("SYS_TTL_LEN", "0", true);//从<?xml version="1.0" encoding="UTF-8" ?>到</TX>的长度
        xmlWriter.writeElementString("SYS_REQ_SEC_ID", getBankConfig().getValueAsString("remoteSecNode"));
        xmlWriter.writeElementString("SYS_SND_SEC_ID", getBankConfig().getValueAsString("localSecNode"));//服务版本号105005
        xmlWriter.writeElementString("SYS_TX_CODE", txCode);
        xmlWriter.writeElementString("SYS_TX_TYPE", "020000");
        xmlWriter.writeElementString("SYS_TX_VRSN", "01");
        xmlWriter.writeElementString("SYS_EVT_TRACE_ID", getQryTransID()+"00000001");
        xmlWriter.writeElementString("SYS_SND_SERIAL_NO", Format.DateAndHourFormat.format(date));
        xmlWriter.writeElementString("SYS_PKG_TYPE", "1");
        xmlWriter.writeElementString("SYS_MSG_LEN", "0", true);//<TX_BODY>节点内容的长度（包括节点本身）。
        xmlWriter.writeElementString("SYS_IS_ENCRYPTED", "3");
        xmlWriter.writeElementString("SYS_ENCRYPT_TYPE", "3");
        xmlWriter.writeElementString("SYS_COMPRESS_TYPE", "0");
        xmlWriter.writeElementString("SYS_EMB_MSG_LEN", "0");
        xmlWriter.writeElementString("SYS_REQ_TIME", Format.DateTime17SSSFormat.format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, 1);// 24小时制
        date = cal.getTime();
        xmlWriter.writeElementString("SYS_TIME_LEFT", Format.TimeSSFormat.format(date));
        xmlWriter.writeElementString("SYS_PKG_STS_TYPE", "00"); //发起方流水号
        xmlWriter.writeEndElement(); // TX_HEADER
        xmlWriter.writeStartElement("TX_BODY");
        xmlWriter.writeStartElement("ENTITY");
    }
    protected void writeCommonHeader(XmlTextWriter xmlWriter, ABC02Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.CCTransCode)) {
            throw new Exception("未赋值CCTransCode");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.ReqSeqNo)) {
            throw new Exception("未赋值ReqSeqNo");
        }
        xmlWriter.writeStartDocument();
        xmlWriter.writeStartElement("ap");
        if("C410".equals(p_bankHeader.CCTransCode) || "1972".equals(p_bankHeader.CCTransCode)){
            xmlWriter.writeElementString("TransCode", p_bankHeader.CCTransCode);
        }else{
            xmlWriter.writeElementString("CCTransCode", p_bankHeader.CCTransCode);
        }
        xmlWriter.writeElementString("ProductID", "ICC");
        xmlWriter.writeElementString("ChannelType", "ERP");
        xmlWriter.writeElementString("CorpNo", bankFront.org);//企业技监局号码/客户号
        xmlWriter.writeElementString("OpNo", bankFront.operator);//企业操作员编号
        xmlWriter.writeElementString("AuthNo","");
        xmlWriter.writeElementString("ReqSeqNo", p_bankHeader.ReqSeqNo);
        if (p_bankHeader.ReqDateTime == null) {
            p_bankHeader.ReqDateTime = new Date();
        }
        xmlWriter.writeElementString("ReqDate", Format.Date8Format.format(p_bankHeader.ReqDateTime));
        xmlWriter.writeElementString("ReqTime", Format.Time6Format.format(p_bankHeader.ReqDateTime));
        xmlWriter.writeElementClosed("Sign");
    }
    public String getAbcAccNo(String p_actNum) {
        //         2省市(-)15账号或者15账号-2省市
        if ((p_actNum.indexOf('-') == 2 && p_actNum.length() == 18) || (p_actNum.length() == 17)) {
            return p_actNum.substring(p_actNum.length() - 15);
        } else if(p_actNum.indexOf('-') ==15 && p_actNum.length() == 18) {
            return p_actNum.substring(0,15);
        }
        //考虑多级账簿的情况   2省市(-)15账号10账薄
        else if((p_actNum.indexOf('-') == 2 && p_actNum.length() == 28) || (p_actNum.length() == 27)){
            return p_actNum.substring(p_actNum.length() - 25,p_actNum.length() - 10);
        }
        //考虑子账户是二级账簿的情况    2省市15账号-10账薄
        else if(p_actNum.indexOf('-') ==17 && p_actNum.length() == 28) {
            return p_actNum.substring(2,17);
        }
//        15账号10账薄
        else if( p_actNum.length() == 25) {
            return p_actNum.substring(0,15);
        }
        return p_actNum;
    }
    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        String url = bankFront.serverURL+"/"+txCode;
        WriteBankLogLn("2.提交银行的地址：" + url);
        WriteBankLogStep2(p_reqStr);
        byte[] tmpToWriteBytes = p_reqStr.getBytes(getCharset());
        WebRequest tmpWebRequest = new WebRequest(url);
        tmpWebRequest.setConnectTimeout(600000);
        tmpWebRequest.setRequestProperty("Content-Type", "application/xml");
        tmpWebRequest.setRequestProperty("Connection", "keep-alive");
        retStr = tmpWebRequest.upload(tmpToWriteBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }
}
